package com.binhai.springboot.login;

import com.binhai.springboot.person.bean.PersonBean;
import com.binhai.springboot.user.bean.UserBean;
import com.binhai.springboot.user.service.UserService;
import com.binhai.springboot.utils.ValidateImageCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Controller
public class LoginController {

    @Autowired
    private UserService service;


    @RequestMapping("/")
    public String toLogin(){
        return "login";
    }

    /**
     * 登录
     */


    @RequestMapping("/login.do")
    public String login(UserBean bean, String code, PersonBean beanbean){
        return service.login(bean,code,beanbean);
    }

    // 生成验证码
    @GetMapping("/code")
    public void getImage(HttpSession session, HttpServletResponse response) throws IOException {
        // 生成验证码
        String securityCode = ValidateImageCodeUtils.getSecurityCode();
        BufferedImage image = ValidateImageCodeUtils.createImage(securityCode);
        // 存入session作用域中
        session.setAttribute("code", securityCode);
        // 响应图片
        ServletOutputStream os = response.getOutputStream();
        ImageIO.write(image, "png", os);
    }

}
